from tools.db import *
import numpy as np
import matplotlib.pyplot as plt
from pyecharts import Pie
import os

'''数据分析、机器学习、人工智能相关职位对学历的要求'''

def education(keywords):

    Post = lagou_post_info


    a = '%' + keywords[0] + '%'
    b = '%' + keywords[1] + '%'
    c = '%' + keywords[2] + '%'


    mydata = Post.select(Post.education, fn.COUNT(Post.id).alias('num')).where(Post.post_name % a| Post.post_name % b| Post.post_name % c).group_by(Post.education)

    labels = ['博士','硕士', '本科', '大专', '不限']
    number = [0]*len(labels)
    for item in mydata:
        if item.education not in labels:
            number[-1] += item.num
        else:
            dex = labels.index(item.education)
            number[dex] = item.num

    title = u'{}职位学历要求'.format(keywords[0])
    pie = Pie(title, title_text_size=25, title_pos="center", width=800, height=500)
    pie.add("", labels, number, is_label_show=True, label_text_size=15, legend_text_size=15, legend_orient='vertical', legend_pos='right', levisual_text_color="#fff", symbol_size=10)
    pie.show_config()
    # pie.render(f'{html_path}education.html')
    pie.render('./templates/education.html')

    print(number)
    plt.figure(1, figsize=(5, 6))
    plt.pie(number, labels=labels, autopct='%1.1f%%', pctdistance=0.8, shadow=True,
            startangle=90)  # startangle表示饼图的起始角度
    plt.axis('equal')
    plt.title(title, bbox={'facecolor': '0.9', 'pad': 6})
    # plt.legend(labels, loc='upper right', bbox_to_anchor=(1.1, 1))
    # plt.show()
    # plt.savefig("../static/img/experience.png")
    # plt.figure(1, figsize=(5, 6))
    # colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
    # plt.pie(number, labels=labels, colors=colors, autopct='%1.1f%%', pctdistance=0.8, shadow=True, startangle=90) #startangle表示饼图的起始角度
    # plt.axis('equal')
    # plt.title(title, bbox={'facecolor':'0.9', 'pad':6})
    # # plt.legend()
    # #
    # # plt.render('../templates/educationtext.html')
    plt.savefig("./static/img/education.png")
    # plt.show()

# if __name__ == '__main__':
#     keywords = ['数据', '人工智能', '机器学习']
#     #     word_cloud(keywords)
#     education(keywords)
